<script setup>
import api from '@/api/index.js'
import { message } from 'ant-design-vue';
import { computed, ref, h, provide } from 'vue';
import MyTable from '@/components/MyTable/MyTable.vue';
import { Modal } from 'ant-design-vue';
import IconFont from '@/components/Icon/IconFont.vue';

const props = defineProps({
  // 表格数据总条数
  total: { type: Number, default: 0 },
  // 表格当前页码
  pageNum: { type: Number, default: 1 },
})
// 分页参数
const pagination = computed(() => ({
  total: props.total,
  showSizeChanger: false,
  current: props.pageNum,
  onChange: e => { emits('pageChange', e) }
}))

const emits = defineEmits(['edit', 'pageChange', 'reload'])

// 表格列配置
const columns = ref([
  { ellipsis: true, title: '名称', dataIndex: 'name', key: 'name', },
  { ellipsis: true, title: '图标', key: 'icon', },
  { ellipsis: true, title: '是否路由', dataIndex: 'route', key: 'route', },
  { ellipsis: true, title: '类型', dataIndex: 'type', key: 'type', },
  { ellipsis: true, title: '组件名称', dataIndex: 'component', key: 'component', },
  { ellipsis: true, title: '权限标识', dataIndex: 'perms', key: 'perms', },
  { ellipsis: true, title: '排序', dataIndex: 'orderNum', key: 'orderNum', },
  { ellipsis: true, title: '操作', key: 'set', },
])

// 编辑数据
const edit = (item) => {
  emits('edit', item)
}

//删除数据
const del = item => {
  Modal.warning({
    title: '删除菜单',
    content: h('span', {}, '是否删除菜单数据：',
      h('span', { style: { color: '#09c690' } }, item.name),
    ),
    okText: '删除',
    okCancel: true,
    okButtonProps: { type: 'primary', danger: true },
    cancelButtonProps: { type: 'primary' },
    cancelText: '取消',
    onOk: async () => {
      await api.delMenu({ id: item.id })
      message.success('删除成功');
      emits('reload')
      return Promise.resolve()
    },
    onCancel: () => {
      message.success('取消删除');
      return Promise.resolve()
    }
  });

}
provide('columns', columns.value)
</script>

<template>
  <MyTable :pagination="pagination" :scroll="{ y: 'calc(100vh - 383px)' }">
    <template #bodyCell="{ column, record }">
      <template v-if="column.key === 'icon'">
        <IconFont :type="record.icon" />
      </template>
      <template v-if="column.key === 'route'">
        {{ Number(record.route) ? '路由' : '其他' }}
      </template>
      <template v-if="column.key === 'type'">
        {{ ['', '菜单', '子菜单', '按钮'][Number(record.type)] }}
      </template>
      <template v-if="column.key === 'set'">
        <AButton type="link" @click="edit(record)">编辑</AButton>
        <AButton type="link" class="com-delbtn" @click="del(record)">删除</AButton>
      </template>
    </template>
  </MyTable>
</template>